package com.gitee.ywj1352.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;

import java.util.HashMap;

/**
 * @author wenjie.yang on 2019/6/21.
 */
@Configuration
public class KafkaProducerConfig {

    @Bean
    public DefaultKafkaProducerFactory getKafkaProducerFactory(){
        HashMap<String, Object> params = new HashMap<>();
        params.put("bootstrap.servers","127.0.0.1:9092");
        params.put("group.id","KafkaProducer_group1");
        params.put("retries","1");
        params.put("batch.size","16384");
        params.put("linger.ms","1");
        params.put("key.serializer","org.apache.kafka.common.serialization.IntegerSerializer");
        params.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");
        DefaultKafkaProducerFactory factory = new DefaultKafkaProducerFactory(params);
        return factory;
    }


    @Bean
    public KafkaTemplate getKafkaTemplate(DefaultKafkaProducerFactory kafkaProducerFactory){
        KafkaTemplate kafkaTemplate = new KafkaTemplate(kafkaProducerFactory,true);
        kafkaTemplate.setDefaultTopic("defaultTopic");
        return kafkaTemplate;
    }


}
